package com.sxz.sbs.module.client;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.blankj.utilcode.util.ToastUtils;
import com.sxz.sbs.R;
import com.sxz.sbs.applibrary.component.ClearEditText;
import com.sxz.sbs.applibrary.dialog.input.ValueInputBoardDialog;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.constant.IntentConstant;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.bean.ClientStallListBean;
import com.sxz.sbs.module.client.presenter.AddStallPresenter;
import com.sxz.sbs.module.client.view.AddStallView;
import com.sxz.sbs.module.sale.mode.StallTypeBean;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hp
 * @date 2018/1/12
 * 编辑  添加车位
 */

public class AddStallActivity extends BaseActivity<AddStallPresenter> implements AddStallView {
    @BindView(R.id.cet_stall_number)
    ClearEditText cetStallNumber;
    @BindView(R.id.cet_stall_type)
    TextView cetStallType;
    @BindView(R.id.cet_stall_price)
    TextView cetStallPrice;
    @BindView(R.id.cet_stall_remark)
    ClearEditText cetStallRemark;
    private int customId;
    private String price = "";
    private ClientStallListBean stallListBean;
    private int stallTypeId;
    private ArrayList<ArrayList<StallTypeBean>> carType2 = new ArrayList<>();
    private ArrayList<StallTypeBean> carType1 = new ArrayList<>();
    private OptionsPickerView stallPicker;
    @Override
    protected void init() {
        customId = getIntent().getIntExtra(IntentConstant.CUSTOM_ID,0);
        stallListBean = getIntent().getParcelableExtra("stall_bean");
        if (stallListBean != null) {
            new CommonTitle.Builder(this).setTitle(getString(R.string.title_modify_stall)).showMore(false).showRightText(true).build().show();
            price = stallListBean.getTxt_SpacePrice();
            cetStallNumber.setText(stallListBean.getTxt_ParkSpaceNum());
            cetStallPrice.setText(stallListBean.getTxt_SpacePrice()+"元");
            cetStallRemark.setText(stallListBean.getTxt_Remark());
            stallTypeId = stallListBean.getVal_SpaceType();
            cetStallType.setText(stallListBean.getSpaceTypeName());
        } else {
            new CommonTitle.Builder(this).setTitle(getString(R.string.title_add_stall)).showMore(false).showRightText(true).build().show();
        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_stall;
    }

    @Override
    protected AddStallPresenter createPresenter() {
        return new AddStallPresenter();
    }


    @OnClick({R.id.tv_title_right,R.id.cet_stall_type, R.id.cet_stall_price})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cet_stall_type:
                if (carType1.size() > 0) {
                    stallPicker.show();
                } else {
                    getCarPlaceType();
                }
                break;
            case R.id.cet_stall_price:
                ValueInputBoardDialog dialog1 = new ValueInputBoardDialog(this,price,ValueInputBoardDialog.UnitEnum.SELL);
                dialog1.setOnDialogResultListener(new ValueInputBoardDialog.OnDialogResultListener() {
                    @Override
                    public void onResult(String value, String unit) {
                        price = value;
                        cetStallPrice.setText(value + unit);
                    }
                });
                dialog1.show();
                break;
            case R.id.tv_title_right:
                if (TextUtils.isEmpty(cetStallNumber.getInputValue())) {
                    cetStallNumber.setShakeAnimation();
                    return;
                }
                if (TextUtils.isEmpty(cetStallType.getText().toString())) {
                    ToastUtils.showShort("车位类型不能为空");
                    return;
                }
                HashMap<String, Object> map = HttpParamMap.getParamMap();
                if (stallListBean != null) {
                    map.put("Custom_Id", stallListBean.getCustom_Id());
                    map.put("Id", stallListBean.getId());
                } else {
                    map.put("Custom_Id", customId);
                }
                map.put("txt_ParkSpaceNum", cetStallNumber.getInputValue());
                map.put("txt_SpacePrice", price);
                map.put("val_SpaceType",stallTypeId);
                map.put("txt_Remark", cetStallRemark.getInputValue());
                mPresenter.addStall(map);
                break;
            default:
                break;
        }
    }
    private void getCarPlaceType() {
        mPresenter.getStallType(HttpParamMap.getParamMap());
    }
    @Override
    public void onAddStallSuccess() {
        finish();
    }

    @Override
    public void onGetStallTypeSuccess(List<StallTypeBean> data) {
        if (mIsLive && data != null) {
            for (int i = 0; i < data.size(); i++) {
                carType1.add(new StallTypeBean(data.get(i).getValue()
                        ,data.get(i).getName()));
                ArrayList<StallTypeBean> type1 = new ArrayList<>();
                for (int j = 0; j < data.get(i).getData().size(); j++) {
                    type1.add(new StallTypeBean
                            (data.get(i).getData().get(j).getValue(),data.get(i).getData().get(j).getName()));
                }
                carType2.add(type1);
            }
            initStallTypePicker();
        }
    }
    private void initStallTypePicker() {
        stallPicker = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String tx = carType1.get(options1).getName()+"-"+carType2.get(options1).get(option2).getName() ;
                cetStallType.setText(tx);
                stallTypeId = carType2.get(options1).get(option2).getValue();
            }
        }).isDialog(false).build();
        stallPicker.setPicker(carType1,carType2);
        stallPicker.show();
    }
}
